$(function () {

	var custom = {
	
		stripesScrollSpeed: 20,
	
		init: function () {
		
			//hook up click event to submit button
			$('.subscribe button.submit').click(function () {
				
				//clear all errors
				$('.subscribe input[type=text]').removeClass('error');

				//Just a simple validate just to make sure fields are all filled					
				//It basically loop through all textfield, if it's empty, add error class
				$('.subscribe input[type=text]').each(function () {
					
					if ($(this).val() == '') {
						$(this).addClass('error');
						
					//just to validate the email
					} else if ($(this).data('type') == 'email' && !custom.validateEmail($(this).val())) {
						$(this).addClass('error');
					}
					
				});

				//if no error classes were found
				if ($('.subscribe .error').length == 0) {					
				
					//call submit function
					custom.submit();			
					
				}
				
				return false;			
			});
			
		},
		
		//ajax call for form submission
		submit: function () {
							
			//format form data into a long string
			var formData = $('.subscribe form').serialize();

			//submit it to backend
			$.ajax({
				type: "post",
				url: "submit.php", /* you will need to change this*/
				data: formData,
				beforeSend: function () {
					custom.scrollStripes();		
				},
				success: function(data){
			
					if (data == 1) {
			
						//if browser support CSS3, use CSS3, otherwise just hide and show			
						//check for csstransforms36 generated by modernizr	   
						if ($('html').hasClass('csstransforms3d')) {		
						
							$('.subscribe').addClass('flip');		
							$('.subscribe .subscribe-wrapper').addClass('flipIt');																								
						
						}	else {								
						
							$('.subscribe-form').fadeOut();
							$('.subscribe-success').fadeIn();
						}							   
			
					}
			
				}
			});
			
		},
		
		//function to animate the stripes
		scrollStripes: function () {
		
	    var current = 0;
	 
	    //Calls the scrolling function repeatedly
	    var init = setInterval(function () {
		    
        // 1 pixel row at a time
        current -= 1;
    
        // move the background with backgrond-position css properties
        $('.subscribe .stripes').css("backgroundPosition", current+"px 0");			    
		    
	    }, custom.stripesScrollSpeed);  			
		
		},
		
		//simplest email validation, need to revalidate it via backend
		validateEmail: function (email) {
	    var re = /\S+@\S+\.\S+/;
	    return re.test(email);		
		}
	
	}

	//run the script!
	custom.init();

});